#import "YQPBalanceModel.h"

#import "MYWithdrawBaseListVC.h"

#import "BRStringPickerView.h"

@interface MYWithdrawBaseListVC ()

@property (strong, nonatomic) YQPBalanceModel *model;

@end

@implementation MYWithdrawBaseListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.listTableView.frame = CGRectMake(0, 0, kYQPScreenWidth, kYQPScreenHeight-Height_NavBar-kHeight_Botton_SafeArea-44);
    
    [self getPresentBalanceApi];
}

#pragma mark - 分享赚钱接口
- (void)getPresentBalanceApi
{
   YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendPostRequest:kPOST_SHARE_BALANCE_URL parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
        if (responseObject.code == 0) {
            
            self.model = [YQPBalanceModel modelWithJSON:responseObject.data];
            
            [self.listTableView reloadData];
            
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
        
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
    }];
}

-(void)setupView {
    [super setupView];
    self.listTableView.backgroundColor = kAPPTABLEVIEW_BG_COLOR;
    kYQREGISTERTABLEVIEWNIBCELL(@"MYWithdrawListTextCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYWithdrawListMoneyCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYWithdrawListPasswordCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYSheardPaymentWithdrawDepositCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYLicenseTypeTextCell")

}
#pragma mark - JXCategoryListContentViewDelegate

- (UIView *)listView {
    return self.view;
}

- (UIScrollView *)listScrollView {
    return self.listTableView;
}
#pragma mark - JXCategoryListContentViewDelegate end
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.type == 0) {
        return 6;
    }
    return 5;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if(indexPath.row == 0 || indexPath.row == 1 || (self.type == 0 && indexPath.row == 2)) {
        MYWithdrawListTextCell *textCell = [tableView  dequeueReusableCellWithIdentifier:@"MYWithdrawListTextCell"];
        if (indexPath.row == 0) {
            if (self.type == 0) {
                MYLicenseTypeTextCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"MYLicenseTypeTextCell"];

                [cell.licenseBtn setTitle:kYQLS(@"银行名称") forState:UIControlStateNormal];
                [cell.licenseBtn setTitleColor:HexColor(0x333333) forState:UIControlStateNormal];
                cell.licenseBtn.titleLabel.font = kSystemMedium(15);
                cell.textField.userInteractionEnabled = NO;
                cell.textField.placeholder = kYQLS(@"请选择");
                [cell.licenseBtn addTarget:self action:@selector(chooseBanks) forControlEvents:UIControlEventTouchUpInside];
                return cell;
            }else{
                textCell.titleLb.text = kYQLS(@"PayPal账户");
                textCell.textField.placeholder = kYQLS(@"请输入PayPal账户");
            }

        }else if (indexPath.row == 1) {
            if (self.type == 0) {
                textCell.titleLb.text = kYQLS(@"银行账户号码");
                textCell.textField.placeholder = kYQLS(@"请输入银行账户号码");
            }else{
                textCell.titleLb.text =kYQLS(@"收款人姓名");
                textCell.textField.placeholder = kYQLS(@"请输入收款人姓名");
            }
        }else{
            textCell.titleLb.text = kYQLS(@"收款人姓名");
            textCell.textField.placeholder = kYQLS(@"请输入收款人姓名");
        }
        return textCell;
    }else if ((self.type == 0 && indexPath.row == 3) || (self.type == 1 && indexPath.row == 2)){
        MYWithdrawListMoneyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYWithdrawListMoneyCell"];
        cell.totalMoneyLb.text = [NSString stringWithFormat:@"%@: HKD%@",kYQLS(@"可提现"),self.model.balance];
        return cell;
    }else if ((self.type == 0 && indexPath.row == 4) || (self.type == 1 && indexPath.row == 3)) {
        MYWithdrawListPasswordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYWithdrawListPasswordCell"];
        return cell;
    }else{
        
        MYSheardPaymentWithdrawDepositCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYSheardPaymentWithdrawDepositCell"];
        [cell.withdrawDepositBtn addTapAction:^(id  _Nonnull sender) {
            [self getPresentBalanceWithdrawApi];
        }];
        return cell;
    }

}

#pragma mark - 银行列表弹框
- (void)chooseBanks
{
    NSArray *banksArr = @[kYQLS(@"中国银行(香港)有限公司"),kYQLS(@"交通银行(香港)有限公司"),kYQLS(@"东亚银行有限公司"),kYQLS(@"中信银行国际有限公司"),kYQLS(@"中国建设银行(亚洲)股份有限公司"),kYQLS(@"集友银行有限公司"),kYQLS(@"创兴银行有限公司"),kYQLS(@"花旗银行(香港)有限公司"),kYQLS(@"大新银行有限公司"),kYQLS(@"恒生银行有限公司"),kYQLS(@"富邦银行(香港)有限公司"),kYQLS(@"星展银行(香港)有限公司司"),kYQLS(@"香港上海汇丰银行有限公司"),kYQLS(@"中国工商银行(亚洲)有限公司"),kYQLS(@"华侨永亨银行有限公司"),kYQLS(@"大众银行(香港)有限公司"),kYQLS(@"上海商业银行有限公司"),kYQLS(@"东南全球银行"),kYQLS(@"渣打银行(香港)有限公司"),kYQLS(@"大生银行有限公司"),kYQLS(@"大有银行有限公司"),kYQLS(@"永隆银行有限公司")];
    
    MYLicenseTypeTextCell *cell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

    [BRStringPickerView showStringPickerWithTitle:@"" dataSource:banksArr defaultSelValue:kYQLS(@"中国银行(香港)有限公司") isAutoSelect:NO themeColor:kAPPThemeColor resultBlock:^(id selectValue) {
        
        [cell.textField setText:selectValue];
        
    } cancelBlock:^{
        
    }];
}
#pragma mark - 提现接口
- (void)getPresentBalanceWithdrawApi
{
    NSString *bankName,*bankAccount,*name,*money,*password,*PayPalName;
    if (self.type == 0) {//银行提现
        MYWithdrawListTextCell *bankNameCell = [self.listTableView  cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        if (bankNameCell.textField.text.length == 0) {
            kMBMesHUD(kYQLS(@"请输入银行名称"));
            return;
        }
        bankName = bankNameCell.textField.text;

        MYWithdrawListTextCell *bankAccountCell = [self.listTableView  cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        if (bankAccountCell.textField.text.length == 0) {
            kMBMesHUD(kYQLS(@"请输入银行账号"));
            return;
        }
        bankAccount = bankAccountCell.textField.text;

        MYWithdrawListTextCell *nameCell = [self.listTableView  cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
        if (nameCell.textField.text.length == 0) {
            kMBMesHUD(kYQLS(@"请输入收款人姓名"));
            return;
        }
        name = nameCell.textField.text;

        MYWithdrawListMoneyCell *moneyCell = [self.listTableView  cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
        if (moneyCell.moneyTextField.text.length == 0) {
            kMBMesHUD(kYQLS(@"请输入提现金额"));
            return;
        }
        if (moneyCell.moneyTextField.text.floatValue < 30.0) {
            kMBMesHUD(kYQLS(@"提现金额不得小于30"));
            return;
        }
        money = moneyCell.moneyTextField.text;

        MYWithdrawListPasswordCell *passCell = [self.listTableView  cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]];
        if (passCell.passwordTextField.text.length == 0) {
            kMBMesHUD(kYQLS(@"请输入登录密码"));
            return;
        }
        password = passCell.passwordTextField.text;

    }else{//paypal提现
        
        MYWithdrawListTextCell *paypalCell = [self.listTableView  cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        if (paypalCell.textField.text.length == 0) {
            kMBMesHUD(kYQLS(@"请输入PayPal账号"));
            return;
        }
        PayPalName = paypalCell.textField.text;

        MYWithdrawListTextCell *nameCell = [self.listTableView  cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        if (nameCell.textField.text.length == 0) {
            kMBMesHUD(kYQLS(@"请输入收款人姓名"));
            return;
        }
        name = nameCell.textField.text;

        MYWithdrawListMoneyCell *moneyCell = [self.listTableView  cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
        if (moneyCell.moneyTextField.text.length == 0) {
            kMBMesHUD(kYQLS(@"请输入提现金额"));
            return;
        }
        if (moneyCell.moneyTextField.text.floatValue < 30.0) {
            kMBMesHUD(kYQLS(@"提现金额不得小于30"));
            return;
        }
        money = moneyCell.moneyTextField.text;

        MYWithdrawListPasswordCell *passCell = [self.listTableView  cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
        if (passCell.passwordTextField.text.length == 0) {
            kMBMesHUD(kYQLS(@"请输入登录密码"));
            return;
        }
        password = passCell.passwordTextField.text;

    }
    

    
    NSMutableDictionary *params = NSMutableDictionary.new;
    if (self.type == 0) {
        params[@"bankName"] = bankName?:@"";//银行名称
        params[@"bankAccount"] = bankAccount?:@"";//银行账号
    }else{
        params[@"paypalAccount"] = PayPalName;//paypal收款账号

    }
    
    params[@"payeeName"] = name;//收款人姓名
    params[@"money"] = money;
    params[@"transferType"] = @(self.type);
    params[@"password"] = password;

   YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendPostRequest:kPOST_WITHDRAW_SUBMIT_URL parameters:params success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
        if (responseObject.code == 0) {
            MYShopSalesOrderSucceedVC *succeedVC = [[MYShopSalesOrderSucceedVC alloc]initWithNibName:@"MYShopSalesOrderSucceedVC" bundle:nil];

            succeedVC.navigationItem.title = kYQLS(@"提交成功");
            succeedVC.paySuceedLbText = kYQLS(@"我们收到您的申请 , 审核成功后与您联系");
            succeedVC.paySuceedBtnText = kYQLS(@"返回个人主页");
            [self.navigationController pushViewController:succeedVC animated:true];

        }else{
            YQUITIPSERROR(responseObject.msg)
        }
        
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ((self.type == 0 && indexPath.row == 3) || (self.type == 1 && indexPath.row == 2)) {
        return 140;
    }else if ((self.type == 0 && indexPath.row == 5) || (self.type == 1 && indexPath.row == 4)) {
        return 188;
    }
    return 58;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
@end
